<template>
  <div class="set-class">
    <div class="el-header">
      <el-menu background-color="#eee" text-color="#303133" class="el-menu-demo" mode="horizontal">
        <el-menu-item index="/home">首页</el-menu-item>
        <el-menu-item index="/2">账号管理页面</el-menu-item>
      </el-menu>
    </div>
    <!-- 主体区域 -->
    <div>
      <!-- 头部查询 -->
      <el-form class="myform" ref="accountFormRef" :model="form" inline>
        <span class="set-span">
          <el-form-item label="账号" prop="account">
            <el-input v-model="form.account"></el-input>
          </el-form-item>
        </span>

        <span class="set-span">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </span>

        <span class="set-span">
          <el-form-item label="手机号" prop="number">
            <el-input v-model="form.number"></el-input>
          </el-form-item>
        </span>
        <el-button size="small" @click="onSubmit">查询</el-button>
        <el-button size="small" @click="clearInput">重置</el-button>
      </el-form>
    </div>

    <!-- 新增账号按钮 -->
    <div>
      <el-button class="el-btn" size="small " @click="showDialog()">新增账号</el-button>
    </div>

    <!-- 账号面版 -->
    <div class="el-account">
      <el-table class="el-tab" :data="tableData" height="500">
        <el-table-column prop="account" label="账号"> </el-table-column>
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="phone" label="联系电话"> </el-table-column>
        <el-table-column prop="post" label="职位"> </el-table-column>
        <!-- switch 开关 -->
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.state" @change="statdChange(scope.row)"> </el-switch>
          </template>
        </el-table-column>
        <!-- 增删改查按钮 -->
        <el-table-column prop="handle" label="操作" width="300px">
          <template slot-scope="scope">
            <!-- 提示文字 在上方 -->
            <el-tooltip class="item" effect="dark" content="编辑" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" circle @click="showEdiDialog(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="重置密码" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-refresh-right" circle @click="resetEdiDialog(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" circle @click="removeUserById(scope.row.id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 重置密码对话框 -->
    <el-dialog title="修改密码" :visible.sync="resetDialogVisible" width="30%">
      <!-- 内容主体区域 -->
      <el-form :model="editForm" ref="editFormRef" label-width="70px">
        <el-form-item label="原密码" prop="password">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="newPassword"></el-input>
        </el-form-item>
        <el-form-item label="再次输入密码" prop="newPassword2">
          <el-input v-model="newPassword2"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="resetDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="resetUserPassword">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog title="修改用户" :visible.sync="editdialogVisible" width="30%" @close="ediDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="editForm" ref="ediFormRef" label-width="70px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="editForm.account" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="post">
          <el-input v-model="editForm.post"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="ediUserInfo">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加用户的对话框 -->
    <el-dialog title="新增账号" :visible.sync="dialogVisible" width="30%" @close="addDialogClose">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" ref="addFormRef" label-width="70px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="addForm.account"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="post">
          <el-input v-model="addForm.post"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'setAccount',
  props: [''],
  data() {
    return {
      // 筛选的表单
      form: {
        account: '',
        name: '',
        number: ''
      },
      // 获取的所有数据
      tableData: [],

      // 搜索对象
      queryInfo: {
        account: '',
        name: '',
        number: ''
      },
      // 控制添加用户 对话框显示与隐藏
      dialogVisible: false,

      // 添加用户表单 数据
      addForm: {
        account: '',
        name: '',
        password: '',
        phone: '',
        post: ''
      },
      // 控制编辑用户 对话框显示与隐藏
      editdialogVisible: false,
      // 编辑用户表单查询到的用户信息对象
      editForm: {},
      oldpassword: '',
      newPassword: '',
      newPassword2: '',
      // 重置密码 对话框显示与隐藏
      resetDialogVisible: false
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    // 获取数据
    async getUserList() {
      const { data } = await this.$http.get('/accountNumbers')
      this.tableData = data
    },

    // switch 开关状态
    async statdChange(userInfo) {
      const data = await this.$http.put(`/accountNumbers/${userInfo.id}`, userInfo)
      if (data.status !== 200) {
        userInfo.state = !userInfo.state
        return this.$message.error('更新用户状态失败!')
      }
      this.$message.success('更新用户状态成功!')
      this.getUserList()
    },

    // 按条件查询
    async onSubmit() {
      // 如果没有选择或者输入任何条件,查询所有的数据
      if (this.form.name + this.form.account + this.form.number === '') {
        return this.getUserList()
      }
      // 先将数据清空
      this.tableData = []
      // 根据用户名查询
      if (this.form.name) {
        const { data } = await this.$http.get(`/accountNumbers?name_like=${this.form.name}`)
        this.tableData = data
      }
      // 根据账号查询
      if (this.form.account) {
        const { data } = await this.$http.get(`/accountNumbers?account_like=${this.form.account}`)
        this.tableData = data
      }
      // 根据号码查询
      if (this.form.number) {
        const { data } = await this.$http.get(`/accountNumbers?number_like=${this.form.number}`)
        this.tableData = data
      }
    },

    // 清空表单事件
    clearInput() {
      this.$refs.accountFormRef.resetFields()
      this.account = ''
      this.name = ''
      this.number = ''
    },
    // 关闭对话框,清空表单内容
    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    // 添加用户
    addUser() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起添加用户的网络请求
        const data = await this.$http.post('/accountNumbers', this.addForm)
        if (data.status !== 201) {
          return this.$message.error('添加用户失败!')
        }
        this.$message.success('添加用户成功!')
        // 隐藏添加用户的对话框
        this.dialogVisible = false
        // 重新获取用户列表
        this.getUserList()
      })
    },
    // 展示添加用户对话框
    showDialog() {
      this.dialogVisible = true
    },
    // 展示编辑用户对话框
    async showEdiDialog(row) {
      this.editForm = row
      this.editdialogVisible = true
    },
    // 显示重置密码对话框
    resetEdiDialog(row) {
      this.editForm = row
      this.oldpassword = this.editForm.password
      this.editForm.password = ''
      this.resetDialogVisible = true
    },
    // 监听编辑行业对话框的关闭事件
    ediDialogClosed() {
      this.$refs.ediFormRef.resetFields()
    },
    // 编辑用户信息并提交
    ediUserInfo() {
      this.$refs.ediFormRef.validate(async () => {
        // 发起修改用户信息的数据请求
        await this.$http.put(`/accountNumbers/${this.editForm.id}`, this.editForm)
        // 关闭对话框
        this.editdialogVisible = false
        // 刷新列表
        this.getUserList()
        this.$message.success('更新用户信息成功!')
      })
    },
    // 重置密码
    resetUserPassword() {
      // 1.检查旧的密码对不对
      if (this.oldpassword !== this.editForm.password) {
        return this.$message.error('您输入的旧密码有误')
      }
      // 2.判断两次输入的新密码是否一致
      if (this.newPassword !== this.newPassword2) {
        return this.$message.error('您输入的两次新密码不一致')
      }
      // 发起请求修改密码
      this.editForm.password = this.newPassword
      this.$refs.editFormRef.validate(async () => {
        // 发起修改用户信息的数据请求
        await this.$http.put(`/accountNumbers/${this.editForm.id}`, this.editForm)

        // 关闭对话框
        this.resetDialogVisible = false
        // 刷新列表
        this.getUserList()
        this.$message.success('更新用户信息成功!')
      })
    },
    // 删除用户
    removeUserById(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示!', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await this.$http.delete(`/accountNumbers/${id}`)
          if (data.status !== 200) {
            return this.$message.error('删除用户失败!')
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getUserList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

      // this.$message.success('删除用户成功!')
      this.getUserList()
    }
  },

  components: {},

  computed: {},

  beforeMount() {},

  mounted() {},

  watch: {}
}
</script>
<style lang="less" scoped>
.set-class {
  width: 100%;
  .el-header {
    height: 70px !important;
    padding: 0;
    width: 100%;
  }
  /deep/ .myform {
    box-sizing: border-box;
    padding-top: 12px;
    height: 65px;
    margin: 0 20px;
    border: 1px solid #a9a9a9;
    border-radius: 10px;
    .el-input__inner {
      width: 150px;
      height: 33px;
      border: 1px solid #a9a9a9;
    }
    .set-span {
      margin: 0 30px;
    }
    .el-button {
      margin-top: 5px;
      margin-left: 30px;
    }
  }
  .el-btn {
    margin: 10px 0 10px 20px;
    border: 1px solid #a9a9a9;
    border-radius: 5px;
  }
  .el-account {
    // height: 650px;
    border: 1px solid #a9a9a9;
    margin: 0 20px;
    border-radius: 10px;
    padding: 10px;
  }
  /deep/.el-tab {
    // margin: 10px;
    border: 1px solide #a9a9a9;
    // font-size: 14px;
    box-sizing: border-box;
    .cell {
      text-align: center;
    }
  }
}
</style>
